//
//  NDSmallCardRow.swift
//  NyamanDana
//
//  Created by 李永彬 on 2025/10/25.
//

import SwiftUI
import Kingfisher

struct NDSmallCardRow: View {
    let product: ProductListItem
    let isLast: Bool
    
    var body: some View {
        let logoUrl = URL.buildURL(with: product.cutad ?? "")
        let appName = product.fascletterism ?? ""
        ZStack {
            // 渐变色背景
            backgroundGradient
            
            VStack(alignment: .leading, spacing: 8) {

                HStack(spacing: 5) {
                    KFImage(logoUrl)
                        .resizable()
                        .aspectRatio(contentMode: .fill)
                        .frame(width: 30, height: 30)
                        .cornerRadius(6)
                    Text(appName)
                        .font(.system(size: 14, weight: .semibold))
                        .foregroundColor(Color(hex: "#271F24"))
                }
                
                Group {
                    let loanInterest = product.focusel ?? ""
                    let loanTerm = product.glass ?? ""
                    // 右侧信息
                    VStack(alignment: .leading, spacing: 12) {
                        Text(localizedString("Loan amount"))
                            .font(.system(size: 12))
                            .foregroundColor(Color(hex: "#999999"))
                        
                        Text(product.majorization ?? "")
                            .font(.system(size: 24, weight: .bold))
                            .foregroundColor(Color(hex: "#271F24"))
                            .lineLimit(1)
                            .fixedSize(horizontal: false, vertical: true)
                            .minimumScaleFactor(0.2)

                        HStack(spacing: 6) {
                            Text(loanInterest)
                                .font(.system(size: 12))
                                .foregroundColor(Color(hex: "#333333"))
                            
                            Rectangle()
                                .fill(Color(hex: "#333333"))
                                .frame(width: 1, height: 14)
                            
                            Text(loanTerm)
                                .font(.system(size: 12))
                                .foregroundColor(Color(hex: "#333333"))
                            Spacer()
                        }
                    }
                }
                
                Spacer()
                
                getMoneyButton
            }
            .padding(.horizontal, 10)
            .padding(.vertical, 12)
            .frame(maxWidth: .infinity, maxHeight: .infinity)
            .background(Color.clear)
        }
        .aspectRatio(1/1.1, contentMode: .fit)
        .cornerRadius(16)
        .overlay(
            RoundedRectangle(cornerRadius: 16)
                .stroke(Color(hex: "#271F24"), lineWidth: 1)
        )
    }
    
    private var backgroundGradient: some View {
        LinearGradient(
            gradient: Gradient(stops: [
                Gradient.Stop(color: Color(hex: "#FFE6DD"), location: 0.0),
                Gradient.Stop(color: Color(hex: "#FFF0EB"), location: 0.3),  // 中间色
                Gradient.Stop(color: Color(hex: "#FFFFFF"), location: 0.4)
            ]),
            startPoint: .top,
            endPoint: .bottom
        )
        .cornerRadius(12)
    }
    
    var getMoneyButton: some View {
        Text(localizedString(product.politward ?? "Get Money"))
            .font(.system(size: 14, weight: .semibold))
            .foregroundColor(Color(hex: "#FFFFFF"))
            .frame(maxWidth: .infinity, alignment: .bottom)
            .frame(height: 32)
            .background(Color(hex: "#FD632B"))
            .cornerRadius(4.0)
            .contentShape(Rectangle())
    }
}
